package com.horay.springclouddemo.common.http;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.net.URI;

/**
 * 响应工具类
 *
 * @author heyingcheng
 * @date 2020/2/16 19:15
 */
public class Responses {

    public static ResponseEntity<ResultBody> ok() {
        return ResponseEntity.ok().body(ResultBody.ok());
    }

    public static <T> ResponseEntity<ResultBody<T>> ok(T data) {
        return ResponseEntity.ok().body(ResultBody.ok(data));
    }

    public static ResponseEntity<ResultBody> created(URI location) {
        return ResponseEntity.created(location).body(ResultBody.custom(HttpStatus.CREATED));
    }

    public static ResponseEntity<ResultBody> accepted() {
        return ResponseEntity.accepted().body(ResultBody.custom(HttpStatus.ACCEPTED));
    }

    public static ResponseEntity.HeadersBuilder<?> noContent() {
        return ResponseEntity.noContent();
    }

    public static ResponseEntity badRequest() {
        return ResponseEntity.badRequest().body(ResultBody.custom(HttpStatus.BAD_REQUEST));
    }

    public static ResponseEntity.HeadersBuilder<?> notFound() {
        return ResponseEntity.notFound();
    }

    public static ResponseEntity<ResultBody> unprocessableEntity() {
        return ResponseEntity.unprocessableEntity().body(ResultBody.custom(HttpStatus.UNPROCESSABLE_ENTITY));
    }

    public static ResponseEntity<ResultBody> unprocessableEntityMessage(String message) {
        return ResponseEntity.unprocessableEntity().body(ResultBody.custom(HttpStatus.UNPROCESSABLE_ENTITY.value(), message));
    }

    public static <T> ResponseEntity<ResultBody<T>> unprocessableEntity(T data) {
        return ResponseEntity.unprocessableEntity().body(ResultBody.custom(HttpStatus.UNPROCESSABLE_ENTITY, data));
    }

    public static <T> ResponseEntity<ResultBody<T>> custom(int code, String msg, T data) {
        HttpStatus httpStatus = ResultBody.code2HttpStatus(code);
        return ResponseEntity.status(httpStatus).body(ResultBody.custom(code, msg, data));
    }

}
